package com.yjq.programmer.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjq.programmer.bean.CodeMsg;
import com.yjq.programmer.dao.CommentMapper;
import com.yjq.programmer.dao.MusicMapper;
import com.yjq.programmer.dao.UserMapper;
import com.yjq.programmer.domain.Comment;
import com.yjq.programmer.domain.CommentExample;
import com.yjq.programmer.domain.Music;
import com.yjq.programmer.domain.User;
import com.yjq.programmer.dto.*;
import com.yjq.programmer.service.ICommentService;
import com.yjq.programmer.service.IUserService;
import com.yjq.programmer.utils.CommonUtil;
import com.yjq.programmer.utils.CopyUtil;
import com.yjq.programmer.utils.UuidUtil;
import com.yjq.programmer.utils.ValidateEntityUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 杨杨吖
 * @QQ 823208782
 * @WX yjqi12345678
 * @create 2022-11-21 15:28
 */
@Service
@Transactional
public class CommentServiceImpl implements ICommentService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private MusicMapper musicMapper;

    @Resource
    private IUserService userService;

    /**
     * 分页获取评论数据
     * @param pageDTO
     * @return
     */
    @Override
    public ResponseDTO<PageDTO<CommentDTO>> getCommentListByPage(PageDTO<CommentDTO> pageDTO) {
        CommentExample commentExample = new CommentExample();
        // 判断是否进行关键字搜索
        CommentDTO searchCommentDTO = pageDTO.getSearchEntity();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        if(!CommonUtil.isEmpty(searchCommentDTO.getContent())) {
            criteria.andContentLike("%" + searchCommentDTO.getContent() + "%");
        }
        // 不知道当前页多少，默认为第一页
        if(pageDTO.getPage() == null){
            pageDTO.setPage(1);
        }
        if(pageDTO.getSize() == null) {
            pageDTO.setSize(5);
        }
        commentExample.setOrderByClause("create_time desc");
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getSize());
        // 分页查出评论数据
        List<Comment> commentList = commentMapper.selectByExample(commentExample);
        PageInfo<Comment> pageInfo = new PageInfo<>(commentList);
        // 获取数据的总数
        pageDTO.setTotal(pageInfo.getTotal());
        // 将domain类型数据  转成 DTO类型数据
        List<CommentDTO> commentDTOList = CopyUtil.copyList(commentList, CommentDTO.class);
        for(CommentDTO commentDTO : commentDTOList) {
            User user = userMapper.selectByPrimaryKey(commentDTO.getUserId());
            if(null != user) {
                commentDTO.setUserDTO(CopyUtil.copy(user, UserDTO.class));
            } else {
                commentDTO.setUserDTO(new UserDTO());
            }
            Music music = musicMapper.selectByPrimaryKey(commentDTO.getMusicId());
            if(null != music) {
                commentDTO.setMusicDTO(CopyUtil.copy(music, MusicDTO.class));
            } else {
                commentDTO.setMusicDTO(new MusicDTO());
            }
        }
        pageDTO.setList(commentDTOList);
        return ResponseDTO.success(pageDTO);
    }

    /**
     * 删除评论数据
     * @param commentDTO
     * @return
     */
    @Override
    public ResponseDTO<Boolean> removeComment(CommentDTO commentDTO) {
        if(CommonUtil.isEmpty(commentDTO.getId())){
            return ResponseDTO.errorByMsg(CodeMsg.DATA_ERROR);
        }
        Comment comment = commentMapper.selectByPrimaryKey(commentDTO.getId());
        if(comment == null) {
            return ResponseDTO.errorByMsg(CodeMsg.COMMENT_NOT_EXIST);
        }
        Music music = musicMapper.selectByPrimaryKey(comment.getMusicId());
        // 删除评论信息
        if(commentMapper.deleteByPrimaryKey(commentDTO.getId()) == 0){
            return ResponseDTO.errorByMsg(CodeMsg.COMMENT_DELETE_ERROR);
        }
        if(music != null) {
            music.setCommentNum(music.getCommentNum() - 1);
            musicMapper.updateByPrimaryKeySelective(music);
        }
        // 对应音乐减少评论数
        return ResponseDTO.successByMsg(true, "删除成功！");
    }

    /**
     * 发表评论数据
     * @param commentDTO
     * @return
     */
    @Override
    public ResponseDTO<Boolean> submitComment(CommentDTO commentDTO) {
        // 登录信息验证
        UserDTO userDTO = new UserDTO(commentDTO.getToken());
        ResponseDTO<UserDTO> loginUserResponse = userService.getLoginUser(userDTO);
        if(!CodeMsg.SUCCESS.getCode().equals(loginUserResponse.getCode())) {
            return ResponseDTO.errorByMsg(CodeMsg.USER_SESSION_EXPIRED);
        }
        userDTO = loginUserResponse.getData();
        commentDTO.setUserId(userDTO.getId());
        // 进行统一表单验证
        CodeMsg validate = ValidateEntityUtil.validate(commentDTO);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())){
            return ResponseDTO.errorByMsg(validate);
        }
        Comment comment = CopyUtil.copy(commentDTO, Comment.class);
        comment.setId(UuidUtil.getShortUuid());
        comment.setCreateTime(new Date());
        if(commentMapper.insertSelective(comment) == 0) {
            return ResponseDTO.errorByMsg(CodeMsg.COMMENT_SUBMIT_ERROR);
        }
        // 对应的音乐增加评论数
        Music music = musicMapper.selectByPrimaryKey(comment.getMusicId());
        music.setCommentNum(music.getCommentNum() + 1);
        musicMapper.updateByPrimaryKeySelective(music);
        return ResponseDTO.successByMsg(true, "发布成功！");
    }

    /**
     * 小程序根据音乐id获取评论数据
     * @param commentDTO
     * @return
     */
    @Override
    public ResponseDTO<List<CommentDTO>> getCommentByMusicId(CommentDTO commentDTO) {
        if(CommonUtil.isEmpty(commentDTO.getMusicId())) {
            return ResponseDTO.errorByMsg(CodeMsg.DATA_ERROR);
        }
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andMusicIdEqualTo(commentDTO.getMusicId());
        commentExample.setOrderByClause("create_time desc");
        List<Comment> commentList = commentMapper.selectByExample(commentExample);
        List<CommentDTO> commentDTOList = CopyUtil.copyList(commentList, CommentDTO.class);
        for(CommentDTO item : commentDTOList) {
            User user = userMapper.selectByPrimaryKey(item.getUserId());
            item.setUserDTO(CopyUtil.copy(user, UserDTO.class));
        }
        return ResponseDTO.success(commentDTOList);
    }
}
